<?php
namespace Admin\Controller;

class SiteDomainController extends Controller {

    public function doSave() {
        $siteId = I('get.site_id', 0, 'intval');
        $id     = I('get.id',      0, 'intval');

        if(empty($id) && empty($siteId)) {
            showJson(array('s' => false, 'm' => '没有网站编号'));
        }

        $g = $_GET;
        $d = array();

        if(!empty($siteId)) {
            $d['site_id'] = $siteId;
        }

        if(!empty($g['domain'])) {
            $d['s_d_domain'] = $g['domain'];
        }

        if(isset($g['status'])) {
            $d['s_d_status'] = I('get.status', 0, 'intval');
        }

        if(!empty($id)) {
            $d['s_d_id'] = $id;
        }

        $m = D2();

        try {
            $r = $m->save2($d) ? array('s' => true, 'm' => '保存成功') : array('s' => false, 'm' => '保存出错');
        } catch (Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function del() {
        $id = empty($_GET['id']) ? 0 : (int)$_GET['id'];

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整，无法删除数据'));
        }

        $m = D2();
        $r = $m->del($id) ? array('s' => true, 'm' => '删除成功') : array('s' => false, 'm' => '删除出错');

        showJson($r);
    }

    public function checkDomainExists() {
        $id     = I('get.id', 0, 'intval');
        $domain = I('get.domain', null);

        if(empty($domain) || isSafeDomainString($domain)) {
            $r = array('s' => true, 'm' => '域名已存在');
        }

        $m = D2();
        $r = $m->checkDominExists($domain, $id)
            ? array('s' => true, 'm' => '域名已存在')
            : array('s' => false, 'm' => '域名不存在');

        showJson($r);
    }

    /**
     * doAdd
     * 新增域名，返回值为增加成功后的数据行
     *
     * @return void
     */
    public function doAdd() {
        $m = D2();

        $domain = I('get.domain', null);
        $status = I('get.status', 0, 'intval');
        $siteId = I('get.site_id', 0, 'intval');

        $dao = array(
            's_d_domain' => $domain,
            's_d_status' => $status,
            'site_id'    => $siteId,
        );

        try {
            if($r = $m->save2($dao)) {
                if(!$record = $m->getBySDId($r)) {
                    $msg = array('s' => false, 'm' => '未找到新增的域名');
                }

                $msg = array('s' => true, 'd' => array('id' => $record['s_d_id'], 'domain' => $record['s_d_domain'], 'status' => $record['s_d_status']));
            } else {
                $msg = array('s' => false, 'm' => '保存新增域名出错');
            }
        } catch (Exception $e) {
            $msg = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($msg);
    }

    /**
     * doChangeStatus
     * 改变域名状态
     *
     * @return void
     */
    public function doChangeStatus() {
        $dao = array(
            's_d_id'     => I('get.id',     0, 'intval'),
            's_d_status' => I('get.status', 0, 'intval')
        );
        $m = D2();

        try {
            $r = $m->changeStatus($dao)
                ? array('s' => true, 'm' => '更新状态成功')
                : array('s' => false, 'm' => '更新状态出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

}
